<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Live extends MY_Controller {
	function __construct(){
		parent::__construct();
		$this->load->model('lives');
		$this->load->model('videos');
		$this->load->model('courses');
		$this->smarty->assign("menu_id", 1);
	}
	/**
	** 网课-视频列表
	*/
	public function index($live_status = 0, $limit = 0)
	{
		
		//即将直播
		/*$waitting = $this->lives->get_jijiangzhibo_list();
		$this->smarty->assign("waitting", $waitting);

		//直播中
		$starting = $this->lives->get_live_starting();
		$this->smarty->assign("starting", $starting);

		//历史直播
		$lishi_list = $this->lives->get_lishi_gzhibo_list();
		$this->smarty->assign("lishi_list", $lishi_list);*/
		$conditions['live_status'] = $live_status;
		$live_list = $this->lives->get_live_list($limit, $this->offset, $conditions);

		$this->load->library('pagination');
		$page_config['base_url'] = '/live/index/'.$live_status;
		$page_config['total_rows'] = $live_list['count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("live_list", $live_list['result']);

		$zhibo = $this->lives->get_last_zhibo();
		if($zhibo){
			$time = time();
			if($zhibo['start_time'] < $time && $time < $zhibo['end_time']){
				$zhibo['live_status'] = 2;
			}
			if($zhibo['start_time'] > $time){
				$zhibo['live_status'] = 1;
			}
			if($zhibo['end_time'] < $time){
				$zhibo['live_status'] = 3;
			}
			$this->smarty->assign("zhibo", $zhibo);
		}

		$this->smarty->assign("menutag", "course");
		$this->smarty->assign("live_status", $live_status);
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->display("course/live_list.html");

	}

	/**
  	* 直播课详情
  	*/
  	public function detail($id)
  	{
  		$info = $this->videos->get_course_info($id);
  		$school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
		//判断直播是否结束；
		$live_status = $this->lives->get_live_status($id);
		if(empty($live_status)){
			$live_status = 1;
		}else{
			$live_status = 0;
		}
		if($this->user_data['id'] > 0){
			$check = $this->lives->check_collect_status($this->user_data['id'],$id,2);
			if($check){
				$info['is_collect'] = 1;
			}else{
				$info['is_collect'] = 0;
			}
		}else{
			$info['is_collect'] = 0;
		}

		/*$order_buy = $this->courses->check_is_buy($id,2);
		if(!empty($order_buy)){
			$effective_time = $order_buy['add_time'] + $order_buy['effective_time']*24*60*60;
			$time = time();
			if($time < $effective_time){
				$is_buy = 1;
			}else{
				$is_buy = 2;
			}
		}else{
			$is_buy = 0;
		}*/
		$is_buy = 0;
		if($this->user_data['id']){
			$order_buy = check_effective_time($this->user_data['id'], $id, 2);
			if($order_buy || ( isset($info['is_free']) && $info['is_free'] == 1 && get_vip_endtime($this->user_data['id']) > time())){
				$is_buy = 1;
			}
		}
		
		$this->smarty->assign("info", $info);
		$this->smarty->assign("is_buy", $is_buy);
		$this->smarty->assign("live_status", $live_status);
		$this->smarty->assign("menutag", "course");
		$this->smarty->display("course/live_detail.html");
  	}

  	function play($id){
  		$info = $this->lives->get_course_info($id);

  		$this->config->load('ws_config');
        $ws_config  = $this->config->item('ws_url_config');
        $info['live_url'] = $ws_config['pre_ws_rtmp_video_url'].$info['stream_id'];
        $school_course_count = $this->videos->get_school_course_count($info['sid']);
		$xuesheng_count = $this->videos->get_school_xuesheng_count($info['sid']);
		$info['course_count'] = $school_course_count;
		$info['xuesheng_count'] = $xuesheng_count;
  		$this->smarty->assign("info", $info);
	    $this->smarty->display("course/live_play.html");
  	}


  	function focus_teacher(){
  		$post_data = $this->input->post();
  		if(isset($post_data['id']) && $post_data['id'] > 0){
  			$check = $this->lives->check_focus_status($this->user_data['id'],$post_data['id']);
  			if($check){
  				$this->db->delete("focus_teacher",array('from_uid'=>$this->user_data['id'],'teacher_id'=>$post_data['id']));
  				echo 2;exit;
  			}
  			$data['from_uid'] = $this->user_data['id'];
  			$data['teacher_id'] = $post_data['id'];
  			$data['add_time'] = time();
  			$result = $this->db->insert("focus_teacher",$data);
  			if($result){
  				echo 1;exit;
  			}else{
  				echo 0;exit;
  			}
  		}else{
  			echo 0;exit;
  		}
  	}

  	function collect(){
  		$post_data = $this->input->post();
  		if(isset($post_data['id']) && $post_data['id'] > 0){
  			$check = $this->lives->check_collect_status($this->user_data['id'],$post_data['id'],$post_data['type']);
  			if($check){
  				echo 2;exit;
  			}
  			$data['uid'] = $this->user_data['id'];
  			$data['top_id'] = $post_data['id'];
  			$data['type'] = $post_data['type'];
  			$data['add_time'] = time();
  			$result = $this->db->insert("user_collect",$data);
  			if($result){
  				echo 1;exit;
  			}else{
  				echo 0;exit;
  			}
  		}else{
  			echo 0;exit;
  		}
  	}
}
